#!/bin/bash

# 检查参数数量
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <author_name> <new_name> <new_email>"
    exit 1
fi

# 参数
AUTHOR_NAME=$1
NEW_NAME=$2
NEW_EMAIL=$3
GIT_FILTER_REPO_PATH="git_filter_repo.py"  # git-filter-repo 的路径

# 验证 git-filter-repo 是否存在
if [ ! -f "$GIT_FILTER_REPO_PATH" ]; then
    echo "Error: git-filter-repo not found at $GIT_FILTER_REPO_PATH"
    exit 1
fi

# 执行 git-filter-repo
python "$GIT_FILTER_REPO_PATH" --commit-callback "
import sys
if commit.author_name.decode('utf-8') == '$AUTHOR_NAME':
    commit.author_name = '$NEW_NAME'.encode('utf-8')
    commit.author_email = '$NEW_EMAIL'.encode('utf-8')
    commit.committer_name = '$NEW_NAME'.encode('utf-8')
    commit.committer_email = '$NEW_EMAIL'.encode('utf-8')
"

# 检查结果
if [ $? -eq 0 ]; then
    echo "Author information modified successfully."
else
    echo "Error occurred while modifying author information."
fi

# input:
# bash modifyAuthor.sh du ddd ddddd  /home/sky-take-out/git_filter_repo.py